/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.grassbelt.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.longview.io.common.config.Global;
import com.longview.io.common.mapper.JsonMapper;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.modules.grassbelt.entity.SysGrassBelt;
import com.longview.io.modules.grassbelt.service.SysGrassBeltService;
import com.longview.io.modules.grasscoverage.entity.GrassCoverage;
import com.longview.io.modules.grasscoverage.service.GrassCoverageService;
import com.longview.io.modules.grasstype.entity.GrassType;

/**
 * 草原带管理Controller
 * @author zf
 * @version 2018-04-17
 */
@Controller
@RequestMapping(value = "${adminPath}/grassbelt/sysGrassBelt")
public class SysGrassBeltController extends BaseController {

	@Autowired
	private SysGrassBeltService sysGrassBeltService;
	@Autowired
	private GrassCoverageService grassCoverageService;	
	@ModelAttribute
	public SysGrassBelt get(@RequestParam(required=false) String id) {
		SysGrassBelt entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysGrassBeltService.get(id);
		}
		if (entity == null){
			entity = new SysGrassBelt();
		}
		return entity;
	}
	
	@RequiresPermissions("grassbelt:sysGrassBelt:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysGrassBelt sysGrassBelt, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SysGrassBelt> page = sysGrassBeltService.findPage(new Page<SysGrassBelt>(request, response), sysGrassBelt); 
		model.addAttribute("page", page);
		return "modules/grassbelt/sysGrassBeltList";
	}
	
	@RequestMapping(value = {"listBelt"})
	public String listBelt(SysGrassBelt sysGrassBelt, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<SysGrassBelt> list = sysGrassBeltService.findList(sysGrassBelt);
		return renderString(response, list);
	}
	@RequiresPermissions("grassbelt:sysGrassBelt:view")
	@RequestMapping(value = "form")
	public String form(SysGrassBelt sysGrassBelt, Model model) {
		SysGrassBelt thisbean = sysGrassBelt;
		sysGrassBelt = sysGrassBeltService.get(sysGrassBelt.getId());
		if (sysGrassBelt == null){
			sysGrassBelt = thisbean;
		}
		List<String> monthList = Lists.newArrayList();
		monthList.add("5");
		monthList.add("6");
		monthList.add("7");
		monthList.add("8");
		monthList.add("9");
		monthList.add("10");
		GrassCoverage grassc = new GrassCoverage();
		grassc.setGrassbeltId(thisbean);
		List<GrassCoverage> grasscoveList = grassCoverageService.getGrassCoverageByBelt(grassc);
		sysGrassBelt.setGrassCoverageList(grasscoveList);
		model.addAttribute("sysGrassBelt", sysGrassBelt);
		model.addAttribute("monthList", monthList);
		return "modules/grassbelt/sysGrassBeltForm";
	}

	@RequiresPermissions("grassbelt:sysGrassBelt:edit")
	@RequestMapping(value = "save")
	public String save(SysGrassBelt sysGrassBelt, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sysGrassBelt)){
			return form(sysGrassBelt, model);
		}
		sysGrassBeltService.save(sysGrassBelt);
		addMessage(redirectAttributes, "保存草原带管理成功");
		return "redirect:"+Global.getAdminPath()+"/grassbelt/sysGrassBelt/?repage";
	}
	
	@RequiresPermissions("grassbelt:sysGrassBelt:edit")
	@RequestMapping(value = "delete")
	public String delete(SysGrassBelt sysGrassBelt, RedirectAttributes redirectAttributes) {
		sysGrassBeltService.delete(sysGrassBelt);
		addMessage(redirectAttributes, "删除草原带管理成功");
		return "redirect:"+Global.getAdminPath()+"/grassbelt/sysGrassBelt/?repage";
	}

	@RequiresPermissions("grassbelt:sysGrassBelt:edit")
	@RequestMapping(value = "deleteMarker")
	public String deleteMarker(SysGrassBelt sysGrassBelt, HttpServletResponse response) {
		sysGrassBeltService.delete(sysGrassBelt);
		sysGrassBelt.setCode("0000");
		sysGrassBelt.setMessage("成功");
		return renderString(response, sysGrassBelt);
	}
	
    @RequestMapping(value = "saveBelt", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody String saveBelt(@RequestBody Map<String, Object> params,HttpServletResponse response) {
    	SysGrassBelt sysGrassBelt = new SysGrassBelt();
    	String id = params.get("id").toString();
    	String name = params.get("name").toString();
    	String grasstypeid = params.get("grasstypeid").toString();
    	String stand8val = params.get("stand8val").toString();
    	String month = params.get("month").toString();    	
    	String standAmount = params.get("standAmount").toString();
    	String lon = params.get("lon").toString();
    	String lat = params.get("lat").toString();
    	String standImg = params.get("standImg").toString();    
    	List<String> coverage = (List<String>)params.get("coverage");
    	List<String> production = (List<String>)params.get("production");
    	List<String> coverageids = (List<String>)params.get("coverageids");
    	sysGrassBelt.setCoverage(coverage);
    	sysGrassBelt.setProduction(production);
    	sysGrassBelt.setCoverageids(coverageids);
    	//JsonMapper.fromJsonString(params,SysGrassBelt.class);
    	sysGrassBelt.setName(name);
    	GrassType gt = new GrassType();
    	gt.setId(grasstypeid);
    	sysGrassBelt.setId(id);
    	sysGrassBelt.setGrasstype(gt);
    	sysGrassBelt.setName(name);
    	sysGrassBelt.setLat(lat);
    	sysGrassBelt.setLon(lon);
    	sysGrassBelt.setStandImg(standImg);
    	sysGrassBelt.setStand8val(stand8val);
    	sysGrassBelt.setStandAmount(standAmount);
    	sysGrassBelt.setMonth(month);
		sysGrassBeltService.save(sysGrassBelt);
		sysGrassBelt.setCode("0000");
		sysGrassBelt.setMessage("成功");
		return renderString(response, sysGrassBelt);
	}
    
    /***
     * 地图查询
     */
	@RequestMapping(value = {"query"})
	public @ResponseBody String query(SysGrassBelt sysGrassBelt, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(StringUtils.isNotBlank(sysGrassBelt.getGrassid()))
		{
			GrassType type = new GrassType();
			type.setId(sysGrassBelt.getGrassid());
			sysGrassBelt.setGrasstype(type);;
		}
		List<SysGrassBelt> list = sysGrassBeltService.findList(sysGrassBelt); 
		return renderString(response, list);
	}
}